package com.hu.jie.mywallpager.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.hu.jie.mywallpager.R;
import com.hu.jie.mywallpager.contanse.UrlString;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment {
    private static final String PARAM1 = "param1";
    private static final String PARAM2 = "param2";
    private static final String PARAM3 = "param3";
    private static final String PARAM4 = "param4";
    private TextView tv;
    private RadioGroup rg;
    private ViewPager vp;
    private List<Fragment> mlist;
    private int intIndex = 0;
    private String text;
    private String mParam1;
    private String mParam2;
    private String mParam3;
    private String mParam4;

    public HomeFragment() {

    }

    public static HomeFragment newInstance(String param1,String param2,String param3,String param4){
        HomeFragment fragment = new HomeFragment();
        Bundle b = new Bundle();
        b.putString(PARAM1,param1);
        b.putString(PARAM2,param2);
        b.putString(PARAM3,param3);
        b.putString(PARAM4,param4);
        fragment.setArguments(b);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments()!=null){
            mParam1 = getArguments().getString(PARAM1);
            mParam2 = getArguments().getString(PARAM2);
            mParam3 = getArguments().getString(PARAM3);
            mParam4 = getArguments().getString(PARAM4);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tv = (TextView) view.findViewById(R.id.textView);
        tv.setText(mParam1);
        rg = (RadioGroup) view.findViewById(R.id.rg_home1);
        vp = (ViewPager) view.findViewById(R.id.viewpager1);
        initData();
        ((RadioButton)rg.getChildAt(intIndex)).setChecked(true);
        vp.setCurrentItem(intIndex);
        vp.setAdapter(new MyAdapter(getFragmentManager()));
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton rb = (RadioButton) group.findViewById(checkedId);
                int index = group.indexOfChild(rb);
                vp.setCurrentItem(index);
            }
        });
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                RadioButton rb = (RadioButton) rg.getChildAt(position);
                rb.setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initData() {
        mlist = new ArrayList<Fragment>();
        Fragment fragment1 = HomeViewPagerFragment.newInstance(mParam2);
        Fragment fragment2 = HomeViewPagerFragment.newInstance(mParam3);
        Fragment fragment3 = HomeViewPagerFragment.newInstance(mParam4);
        mlist.add(fragment1);
        mlist.add(fragment2);
        mlist.add(fragment3);

    }
    public class MyAdapter extends FragmentPagerAdapter{

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mlist.get(position);
        }

        @Override
        public int getCount() {
            return mlist.size();
        }
    }

}
